package com.envper.common.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimestampSerializer extends JsonSerializer<Long> {

    @Override
    public void serialize(Long timestamp, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        timestamp = timestamp * 1000;
        // 格式化日期时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String formattedDate = sdf.format(new Date(timestamp));
        // 写入格式化后的日期时间
        gen.writeString(formattedDate);
    }
}
